package com.rm.zeyou.util.login;

import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.TechnicianMapper;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class TechnicianLoginUtil {
    @Resource
    private TechnicianMapper technicianMapper;


    public Map<String, String> getToken(Long id) {
        //先校验是否封禁
        StpKit.TECHNICIAN.checkDisable(id);
        StpKit.TECHNICIAN.login(id);
        String tokenValue =   StpKit.TECHNICIAN.getTokenValue();
        String tokenName =   StpKit.TECHNICIAN.getTokenName();
        HashMap<String, String> map = new HashMap<>();
        map.put("tokenName", tokenName);
        map.put("tokenValue", tokenValue);
        return map;
    }

    public Technician getTechnician() {
        Long tid =  StpKit.TECHNICIAN.getLoginIdAsLong();
        Technician technician = technicianMapper.selectById(tid);
        if (CheckEmptyUtil.isEmpty(technician)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.TECHNICIAN_EMPTY);
        }

        return technician;
    }

}
